Row Locking? - Mailing list pgsql-sql

From eric soroos
Subject Row Locking?
Date
Msg-id 38241230.1176905921@[4.42.179.151]
Whole thread Raw
Responses Re: Row Locking?
List pgsql-sql
I have a long running process that performs outside actions on the content of a table. The actions could all be done in
parallel(if I had n processors), but I need to ensure that the process is attempted exactly one time per applicable
row.
 

My current design for one thread is the following (simplified to a test case)

create table foo (pending boolean, done boolean, idx serial);

select * from foo where pending='f' and done='f' limit 1;
update foo set pending='t' where idx=[[returned idx]];
commit;

do stuff outside database

update foo set pending='f', done='t' where idx=[[returned idx]];
commit;


Extending this to multiple threads if proving problematic. No locking leads to a race condition between the select and
update.If I change the select to a SELECT ... FOR UPDATE it apparently locks the table against all other select for
updates,then when the update is committed, the second thread returns nothing, even when there are other rows in the
tablethat could be returned.
 

Is there a single row locking against select?  Or can I effeciently do the equivalent of update set pending, then
selectthe row that I just updated to get the contents? (perhaps without doing a table scan to find the oid of the row
thatI just updated). I can't afford to lock the entire table.
 

eric




pgsql-sql by date:

Previous
From: Josh Berkus
Date:
Subject: Re: Delete/Replace Bug in Functions?
Next
From: Vernon Wu
Date:
Subject: Fwd: Re: Can I search for an array in csf?